<template>
  <div class="gantt-chart-container">
    <div class="gantt-header">
      <h3>项目甘特图</h3>
      <div class="gantt-controls">
        <el-button size="small" @click="zoomIn">放大</el-button>
        <el-button size="small" @click="zoomOut">缩小</el-button>
        <el-button size="small" @click="resetZoom">重置</el-button>
      </div>
    </div>
    
    <div class="gantt-content" ref="ganttContainer">
      <!-- 甘特图头部 -->
      <div class="gantt-header-row">
        <div class="gantt-task-header">任务</div>
        <div class="gantt-timeline-header">
          <div 
            v-for="date in timelineDates" 
            :key="date"
            class="timeline-date"
            :style="{ width: `${dayWidth}px` }">
            {{ formatDate(date) }}
          </div>
        </div>
      </div>
      
      <!-- 甘特图内容 -->
      <div class="gantt-body">
        <div 
          v-for="task in tasks" 
          :key="task.id"
          class="gantt-task-row">
          <!-- 任务信息 -->
          <div class="gantt-task-info">
            <div class="task-name">{{ task.name }}</div>
            <div class="task-duration">
              {{ formatDuration(task.start_date, task.due_date) }}
            </div>
          </div>
          
          <!-- 任务时间线 -->
          <div class="gantt-task-timeline">
            <div 
              v-if="task.start_date && task.due_date"
              class="task-bar"
              :class="getTaskStatusClass(task.status)"
              :style="getTaskBarStyle(task)"
              :title="`${task.name} (${task.progress}%)`">
              <div class="task-progress" :style="{ width: task.progress + '%' }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted, watch } from 'vue'

export default {
  name: 'GanttChart',
  props: {
    tasks: {
      type: Array,
      default: () => []
    },
    projectStartDate: {
      type: String,
      default: null
    },
    projectEndDate: {
      type: String,
      default: null
    }
  },
  setup(props) {
    const ganttContainer = ref(null)
    const dayWidth = ref(30) // 每天的宽度
    
    // 计算时间线日期
    const timelineDates = computed(() => {
      if (!props.projectStartDate || !props.projectEndDate) {
        return []
      }
      
      const start = new Date(props.projectStartDate)
      const end = new Date(props.projectEndDate)
      const dates = []
      
      for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
        dates.push(new Date(d))
      }
      
      return dates
    })
    
    // 格式化日期
    const formatDate = (date) => {
      return new Date(date).toLocaleDateString('zh-CN', { 
        month: 'short', 
        day: 'numeric' 
      })
    }
    
    // 格式化持续时间
    const formatDuration = (startDate, endDate) => {
      if (!startDate || !endDate) return ''
      
      const start = new Date(startDate)
      const end = new Date(endDate)
      const diffTime = Math.abs(end - start)
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
      
      return `${diffDays}天`
    }
    
    // 获取任务条样式
    const getTaskBarStyle = (task) => {
      if (!task.start_date || !task.due_date || !props.projectStartDate) {
        return {}
      }
      
      const projectStart = new Date(props.projectStartDate)
      const taskStart = new Date(task.start_date)
      const taskEnd = new Date(task.due_date)
      
      const startOffset = Math.floor((taskStart - projectStart) / (1000 * 60 * 60 * 24))
      const duration = Math.ceil((taskEnd - taskStart) / (1000 * 60 * 60 * 24))
      
      return {
        left: `${startOffset * dayWidth.value}px`,
        width: `${duration * dayWidth.value}px`
      }
    }
    
    // 获取任务状态样式类
    const getTaskStatusClass = (status) => {
      const statusMap = {
        'not_started': 'status-not-started',
        'in_progress': 'status-in-progress',
        'completed': 'status-completed',
        'delayed': 'status-delayed',
        'blocked': 'status-blocked',
        'cancelled': 'status-cancelled'
      }
      return statusMap[status] || 'status-not-started'
    }
    
    // 缩放控制
    const zoomIn = () => {
      dayWidth.value = Math.min(dayWidth.value + 5, 50)
    }
    
    const zoomOut = () => {
      dayWidth.value = Math.max(dayWidth.value - 5, 10)
    }
    
    const resetZoom = () => {
      dayWidth.value = 30
    }
    
    return {
      ganttContainer,
      dayWidth,
      timelineDates,
      formatDate,
      formatDuration,
      getTaskBarStyle,
      getTaskStatusClass,
      zoomIn,
      zoomOut,
      resetZoom
    }
  }
}
</script>

<style scoped>
.gantt-chart-container {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  overflow: hidden;
}

.gantt-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}

.gantt-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.gantt-controls {
  display: flex;
  gap: 5px;
}

.gantt-content {
  overflow-x: auto;
}

.gantt-header-row {
  display: flex;
  border-bottom: 1px solid #e4e7ed;
  background-color: #fafafa;
}

.gantt-task-header {
  width: 200px;
  min-width: 200px;
  padding: 10px;
  font-weight: 600;
  border-right: 1px solid #e4e7ed;
}

.gantt-timeline-header {
  display: flex;
  flex: 1;
}

.timeline-date {
  padding: 10px 5px;
  text-align: center;
  font-size: 12px;
  border-right: 1px solid #f0f0f0;
  min-width: 30px;
}

.gantt-body {
  max-height: 400px;
  overflow-y: auto;
}

.gantt-task-row {
  display: flex;
  border-bottom: 1px solid #f0f0f0;
  min-height: 40px;
}

.gantt-task-row:hover {
  background-color: #f5f7fa;
}

.gantt-task-info {
  width: 200px;
  min-width: 200px;
  padding: 10px;
  border-right: 1px solid #e4e7ed;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.task-name {
  font-weight: 500;
  margin-bottom: 2px;
}

.task-duration {
  font-size: 12px;
  color: #909399;
}

.gantt-task-timeline {
  flex: 1;
  position: relative;
  min-height: 40px;
  display: flex;
  align-items: center;
}

.task-bar {
  position: absolute;
  height: 20px;
  border-radius: 3px;
  background-color: #409eff;
  border: 1px solid #337ecc;
  cursor: pointer;
  transition: all 0.2s;
  overflow: hidden;
}

.task-bar:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.task-progress {
  height: 100%;
  background-color: #67c23a;
  transition: width 0.3s ease;
}

/* 任务状态样式 */
.status-not-started {
  background-color: #909399;
  border-color: #606266;
}

.status-in-progress {
  background-color: #409eff;
  border-color: #337ecc;
}

.status-completed {
  background-color: #67c23a;
  border-color: #529b2e;
}

.status-delayed {
  background-color: #e6a23c;
  border-color: #b88230;
}

.status-blocked {
  background-color: #f56c6c;
  border-color: #c45656;
}

.status-cancelled {
  background-color: #909399;
  border-color: #606266;
  opacity: 0.5;
}
</style>
